<?php
namespace MUD;

class Items{
    private $items=array();
    
    public function __construct(){
        $this->loadItems();
    }
    
    private function loadItems(){
        $fd = new \SimpleXMLElement(file_get_contents(__ROOT__.DS.'res'.DS.'items.xml'));
        
        foreach($fd->item as $item){
            $id = (int) $item['id'];
            $class = (string) $item['class'];
            $name = (string) $item->name;
            $description = (string) $item->description;
            $ground = (string) $item->ground;
            $value = (int) $item->value;
            $spawns = array();
            foreach($item->spawn as $sp){
                $spawns[] = (int) $sp;
            }
            
            if(($sc=count($spawns)) == 1){
                $location = $sc[0];
            }else{
                $location = mt_rand(0, ($sc-1));
            }
            
            $this->items[$id] = new Item($id, $class, $name, $description, $ground, $value, $spawns, $location);
        }
    }
    
    public function getItemByName($name){
        for($i=0,$ic=count($this->items);$i<$ic;$i++){
            if($this->items[$i]->name == $name){
                return $this->items[$i];
            }
        }
    }
    
    public function updateItemLocation($item_id, $location, $location_type){
        $this->items[$item_id]->location=$location;
        $this->items[$item_id]->location_type=$location_type;
    }
}